<script setup lang="ts">
import { h, onMounted, reactive } from 'vue'
import { useI18n } from 'vue-i18n'
import { message, notification, Button } from 'ant-design-vue'
import $config from '@/config'
import router from '@/router'
import { useUserStore } from '@/stores/user'
import ChangeDarkmode from '@/components/ChangeDarkmode.vue'
import LoginForm from './log-form.vue'
import RegistForm from './reg-form.vue'

const { t } = useI18n()

defineOptions({
  name: 'Login',
})

// 用户存储
const userStore = useUserStore()

// 状态参数
const state = reactive({
  // 当前状态
  type: 'login',
  // 提示key
  noteKey: 'mockNotify',
  // 全局loading
  isLoading: true,
})

// 切换状态
const changeType = (type: string) => {
  state.type = type
}

// 登录成功（回调）
const onLogin = (data: LoginForm) => {
  console.log('login data: ', data)
  notification.close(state.noteKey)
  message.success(t('account.login.loginSuccess'))
  router.push({
    name: 'Chat',
  })
}

// 注册成功（回调）
const onRegist = (data: RegistForm) => {
  console.log('reg data: ', data)
  message.success(t('account.login.registerSuccess'))
  changeType('login')
}

onMounted(() => {
  if (userStore.isLogin) {
    router.push({
      name: 'Chat',
    })
    return
  }
  state.isLoading = false
  if ($config.isMockMode) {
    notification.open({
      message: t('account.login.mockTips.title'),
      description: t('account.login.mockTips.description'),
      placement: 'bottomRight',
      btn: () =>
        h(
          Button,
          {
            type: 'primary',
            size: 'small',
            onClick: () => notification.close(state.noteKey),
          },
          { default: () => t('account.login.mockTips.ok') },
        ),
      key: state.noteKey,
      duration: 10000,
    })
  }
})
</script>

<template>
  <div v-if="!state.isLoading" class="login-page">
    <div class="theme"><ChangeDarkmode /></div>
    <div class="login-container">
      <div class="logo-title">
        <h1>{{ $config.title }}</h1>
      </div>
      <template v-if="state.type === 'login'">
        <LoginForm @ok="onLogin" />
        <div class="tips">
          {{ t('account.login.toRegister') }}
          <n-button type="primary" text @click="changeType('regist')">
            {{ t('account.login.registerNow') }}
          </n-button>
        </div>
      </template>
      <template v-else>
        <RegistForm @ok="onRegist" />
        <div class="tips">
          {{ t('account.login.toLogin') }}
          <n-button type="primary" text @click="changeType('login')">
            {{ t('account.login.loginNow') }}
          </n-button>
        </div>
      </template>
    </div>
    <div class="footer">&copy; 2025 Copyright</div>
    <div class="bubble" style="width: 20px; height: 20px; left: 10%; animation-duration: 12s"></div>
    <div class="bubble" style="width: 30px; height: 30px; left: 25%; animation-duration: 9s"></div>
    <div class="bubble" style="width: 15px; height: 15px; left: 40%; animation-duration: 11s"></div>
    <div class="bubble" style="width: 25px; height: 25px; left: 55%; animation-duration: 10s"></div>
    <div class="bubble" style="width: 35px; height: 35px; left: 70%; animation-duration: 8s"></div>
    <div class="bubble" style="width: 18px; height: 18px; left: 85%; animation-duration: 13s"></div>
  </div>
</template>

<style lang="scss" scoped>
.login-page {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

.theme {
  position: absolute;
  top: 25px;
  right: 25px;
  z-index: 2;
}

.login-container {
  position: relative;
  top: -10vh;
  background-color: var(--vas-login-bg);
  padding: 20px 40px;
  border-radius: 8px;
  box-shadow:
    0 0 1px rgba(0, 0, 0, 0.2),
    0 0 4px rgba(0, 0, 0, 0.02),
    0 12px 36px rgba(0, 0, 0, 0.06);
  width: 500px;
  z-index: 1;

  .logo-title {
    text-align: center;
    margin-bottom: 20px;

    h1 {
      font-size: 24px;
      color: var(--vas-title-color);
      margin-bottom: 40px;
    }
  }
}

.tips {
  margin-top: 20px;
  text-align: center;
  font-size: 15px;
  color: var(--vas-text-color);
}

.footer {
  position: absolute;
  z-index: 2;
  bottom: 15px;
  text-align: center;
  width: 100%;
  color: #999;
}

.bubble {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(0, 123, 255, 0.7);
  bottom: -100px;
  animation: bubble-float 10s infinite ease-in-out;
}

@keyframes bubble-float {
  0% {
    bottom: -100px;
    opacity: 1;
  }

  100% {
    bottom: 70vh;
    opacity: 0;
  }
}

@media (max-width: 767px) {
  .login-page {
    align-items: unset;
  }

  .login-container {
    top: 0;
    width: 100%;
    padding: 20px;
    box-shadow: none;
    .logo-title {
      text-align: left;
      margin-right: 60px;
    }
  }

  .bubble {
    display: none;
  }
}
</style>
